;
(function () {

  new Vue({
    el: '.businessCreate',
    data: {
      type: [{ name: '公司', code: 'QY' }, { name: '个人', code: 'PE' }],
      industryData: [],
      selectedIndustry: [],
      selectedIndustryVal: [],
      params: {
        themeInfo: {},
        zlfInfo: {}
      },
      sjId: hlUrlUtil.GetQueryString('businessId'),
      page: hlUrlUtil.GetQueryString('page') || 'businessMange',
      channelTypeData: [], //渠道分类
      selectedChannelType: [],
      selectedChannelTypeVal: [],
      channelNameData: [], //渠道名称
      allchannelNameData: [], //总渠道名称
      selectedChannelName: [],
      selectedChannelNameVal: [],
      sourceData: [],
      selectedSource: [],
      selectedSourceVal: [],
      validataflag: false,
      isShowChannelName: false,
    },
    mounted: function () {
      this.getIndustryData()
      this.getSourceData()
      this.getChannelTypeData()
      this.getChannelNameData()



    },
    methods: {
      //关闭弹窗
      hidePopValidata() {
        this.validataflag = false
      },
      //验证客户
      validataCustomer() {
        if (!this.params.themeInfo.theme) {
          hl.toast('请填写主题')
          return
        }
        if (!this.params.zlfInfo.customerName) {
          hl.toast('请选择客户名称')
          return
        }
        if (!this.params.zlfInfo.lperson) {
          hl.toast('请填写联系人')
          return
        }
        // if (!this.params.zlfInfo.phone) {
        //   hl.toast('请填写联系电话')
        //   return
        // }
        if (this.params.zlfInfo.email) {
          var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
          if (!reg.test(this.params.zlfInfo.email)) {
            hl.toast('邮箱格式不正确')
            this.params.zlfInfo.email = '';
            return
          }
        }

        hlhttp.get({
          url: 'customer/check_bizoppt_repeat',
          data: {
            sj_id: this.sjId,
            customer_name: this.params.zlfInfo.customerName,
            cstmr_contact: this.params.zlfInfo.lperson,
            cstmr_contact_no: this.params.zlfInfo.phone,
            customer_type: this.params.zlfInfo.customerType
          },
          success: (res) => {
            if (res.repeat == 'N') this.addBusiness()
            if (res.repeat == 'Y') this.validataflag = true
          }
        })
      },
      getIndustryData() {
        var self = this
        hlhttp.get({
          url: 'configure/industry/dict',
          success: data => {
            self.industryData = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      selectedCustomerType(val) {
        this.params.zlfInfo.customerType = val;
      },
      getChannelTypeData() {
        var self = this

        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH', config_name: 'ZS_CHANNEL_CATE' },
          success: data => {
            let arr = []
            data.map(item =>{
              if(item.status == 'Y'){
                arr.push(item)
              }
            })
            self.channelTypeData = hlArrayUtil.replaceData(arr, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      getChannelNameData() {
        var self = this

        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH', config_name: 'ZS_CHANNEL_NAME' },
          success: data => {
            self.allchannelNameData = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
            self.getDetails()
          }
        })
      },
      getSourceData() {
        var self = this

        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH0SHJI', config_name: 'SJ_SOURCE' },
          success: data => {
            self.sourceData = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      handleSourceChange(data) {
        this.selectedSourceVal = data[0]['name'].split()
      },
      handleIndustryChange(data) {
        this.selectedIndustryVal = data[0]['name'].split()
      },
      handleChannelTypeChange(data) {
        this.channelNameData = []
        this.allchannelNameData.map(item => {
          if (item.parentDictCode ==  data[0].dictCode) {
            this.channelNameData.push(item)
          }
        })
        this.selectedChannelName = []
        this.selectedChannelTypeVal = data[0]['name'].split()
      },
      handleChannelNameChange(data) {
        this.selectedChannelNameVal = data[0]['name'].split()
      },
      //获取线索详情
      getDetails() {
        var self = this
        hlhttp.get({
          url: 'customer/biz_oppt_info',
          data: { sj_id: self.sjId },
          success: res => {
            self.params = res;

            if (res.zlfInfo.industryCode) self.selectedIndustry.push(res.zlfInfo.industryCode)
            if (res.zlfInfo.industryName) self.selectedIndustryVal.push(res.zlfInfo.industryName)

            if (res.themeInfo.sourceCode) self.selectedSource.push(res.themeInfo.sourceCode)
            if (res.themeInfo.sourceValue) self.selectedSourceVal.push(res.themeInfo.sourceValue)

            if (res.themeInfo.channelCategoryCode) {
              self.selectedChannelType.push(res.themeInfo.channelCategoryCode)
              self.channelNameData = []
              self.allchannelNameData.map(item => {
                if (item.parentDictCode == res.themeInfo.channelCategoryCode) {
                  self.channelNameData.push(item)
                }
              })
            }

            if (res.themeInfo.channelNameCode) {
              let isHold = false
              self.channelNameData.map(item =>{
                if(item.dictCode == res.themeInfo.channelNameCode){
                  isHold = true
                }
              })
              if(isHold) self.selectedChannelName.push(res.themeInfo.channelNameCode)
            }


            if (res.themeInfo.channelCategoryValue) self.selectedChannelTypeVal.push(res.themeInfo.channelCategoryValue)
            if (res.themeInfo.channelNameValue) self.selectedChannelNameVal.push(res.themeInfo.channelNameValue)

          }
        })
      },
      //新建
      addBusiness() {
        const themeInfo = {
          "theme": this.params.themeInfo.theme,
          "source_code": this.selectedSource.length ? this.selectedSource.join() : '#',
          "source_value": this.selectedSourceVal.length ? this.selectedSourceVal.join() : '#',
          "channel_category_code": this.selectedChannelType.length ? this.selectedChannelType.join() : '#',
          "channel_category_value": this.selectedChannelTypeVal.length ? this.selectedChannelTypeVal.join() : '#',
          "channel_name_code": this.selectedChannelName.length ? this.selectedChannelName.join() : '#',
          "channel_name_value": this.selectedChannelNameVal.length ? this.selectedChannelNameVal.join() : '#',
          "description": this.params.themeInfo.description || '#'
        }

        const zlfInfo = {
          "customer_name": this.params.zlfInfo.customerName || '#',
          "customer_type": this.params.zlfInfo.customerType || '#',
          "idcode": this.params.zlfInfo.idcode || '#',
          "industry_code": this.selectedIndustry.length ? this.selectedIndustry.join() : '#',
          "industry_value": this.selectedIndustryVal.length ? this.selectedIndustryVal.join() : '#',
          "lperson": this.params.zlfInfo.lperson || '#',
          "customer_addr": this.params.zlfInfo.customerAddress || '#',
          "phone": this.params.zlfInfo.phone || '',
          "email": this.params.zlfInfo.email || '#',
          "zipcode": this.params.zlfInfo.zipcode || '#'
        }

        hlhttp.put({
          url: 'customer/biz_oppt',
          data: {
            sj_id: this.sjId,
            theme_info: JSON.stringify(themeInfo),
            zlf_info: JSON.stringify(zlfInfo)
          },
          success: () => {
            hl.toast('编辑成功')
            hlUrlUtil.goTo('businessDetails', { businessId: this.sjId, ddtab: true })
          }
        })
      }
    },

  })
}())
